package org.youyee.game.pathFinding
{
	/**
	 * 用于描述一个逻辑位置节点 
	 * @author youyee
	 * @version 2.0
	 */	
	public class PathNode
	{
		public static const DISTANCE:int = 10;
		public static const DISTANCE_2:int = 14;
		
		/**
		 * 横向坐标 
		 */		
		public var x:int;
		
		/**
		 * 纵向坐标 
		 */		
		public var y:int;
		
		/**
		 * 唯一的键值 
		 */		
		public var key:String; 
		
		/**
		 * 顺时针周围的节点 
		 */		
		public var aroundNodesClockWise:Array;
		
		/**
		 * 逆时针周围的节点 
		 */		
		public var aroundNodesCounterClockWise:Array;
		
		/**
		 * 该结点的通过速度 
		 */		
		public var speed:Number = 1;
		
		/**
		 * 目标节点 
		 */		
		public var targetNode:PathNode;
		
		/**
		 * 起始节点 
		 */		
		public var prevNode:PathNode;
		
		/**
		 * 相对于上一个节点的位移
		 */		
		public var g:int;	
		
		/**
		 * 此节点相对于目标节点的曼哈顿距离
		 */		
		public var h:int;
		
		/**
		 * 节点权衡总值 
		 */		
		public var f:int;
		
		private var _isBarrier:Boolean = false;
		
		/**
		 * 构造函数 
		 * @param x 逻辑横轴位置节点
		 * @param y 逻辑纵轴位置节点
		 * 
		 */		
		public function PathNode(x:int=0, y:int=0, setToBarrier:Boolean=false, speed:Number=1)
		{
			this.x = x;
			this.y = y;
			
			this.key = x + "_" + y;
			this.speed = speed;
		}
		
		/**
		 * 获取此点是否为障碍节点 
		 * @return 
		 * 
		 */		
		public function get isBarrier():Boolean
		{
			return this._isBarrier;
		}
		
		/**
		 * 将此节点设置成为障碍节点 
		 * @param value
		 * 
		 */		
		public function set isBarrier(value:Boolean):void
		{
			this._isBarrier = value;
		}
		
		/**
		 * 生成权衡值 
		 * 
		 */		
		public function generateGHF():void
		{
			if (this.targetNode && this.prevNode)
			{
				var pX:int = this.prevNode.x;
				var pY:int = this.prevNode.y;
				
				var tX:int = this.targetNode.x;
				var tY:int = this.targetNode.y;
				
				h = (Math.abs(tX - this.x) + Math.abs(tY - this.y)) * DISTANCE;
				f = (g + h) * speed;
			}
		}
		
		public function toString():String
		{
			return "(" + x + ", " + y + ")";
		}
		
		/**
		 * 销毁此对象 
		 * 
		 */		
		public function destroy():void
		{
			this.prevNode = null;
			this.targetNode = null;
			
			this.aroundNodesClockWise = null;
			this.aroundNodesCounterClockWise = null;
			this.key = null;
		}
	}
}